<template>
  <el-card>
    <el-button size="small" type="primary" @click="handleAdd()">
      新增任务
    </el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="task_id" label="ID" width="80" />
      <el-table-column prop="title" label="任务标题" />
      <el-table-column prop="description" label="任务描述" />
      <el-table-column prop="path_id" label="路径ID" width="100" />
      <el-table-column prop="day_number" label="天数" width="80" />
      <el-table-column prop="is_required" label="是否必填" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.is_required === 1 ? 'success' : 'info'">
            {{ scope.row.is_required === 1 ? '必填' : '选填' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleView(scope.row)">
            查看
          </el-button>
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="您确定要删除吗?" @confirm="handleRemove(scope.row.task_id)">
            <template #reference>
              <el-button size="small" type="warning">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查看对话框 -->
    <el-dialog v-model="viewDialogVisible" title="任务详情" width="50%">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="ID">{{ currentTask.task_id }}</el-descriptions-item>
        <el-descriptions-item label="任务标题">{{ currentTask.title }}</el-descriptions-item>
        <el-descriptions-item label="任务描述">{{ currentTask.description }}</el-descriptions-item>
        <el-descriptions-item label="路径ID">{{ currentTask.path_id }}</el-descriptions-item>
        <el-descriptions-item label="天数">{{ currentTask.day_number }}</el-descriptions-item>
        <el-descriptions-item label="内容">{{ currentTask.content }}</el-descriptions-item>
        <el-descriptions-item label="是否必填">{{ currentTask.is_required === 1 ? '必填' : '选填' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog v-model="editDialogVisible" :title="editDialogTitle" width="50%">
      <el-form :model="currentTask" label-width="80px">
        <el-form-item label="ID">
          <el-input v-model="currentTask.task_id" disabled />
        </el-form-item>
        <el-form-item label="任务标题" required>
          <el-input v-model="currentTask.title" />
        </el-form-item>
        <el-form-item label="任务描述">
          <el-input v-model="currentTask.description" type="textarea" />
        </el-form-item>
        <el-form-item label="路径ID" required>
          <el-input v-model="currentTask.path_id" type="number" />
        </el-form-item>
        <el-form-item label="天数" required>
          <el-input v-model="currentTask.day_number" type="number" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="currentTask.content" type="textarea" />
        </el-form-item>
        <el-form-item label="是否必填">
          <el-select v-model="currentTask.is_required" placeholder="请选择">
            <el-option label="必填" value="1" />
            <el-option label="选填" value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axiosObject from '@/main.js';

const tableData = ref([])
const viewDialogVisible = ref(false)
const editDialogTitle = ref("")
const editDialogVisible = ref(false)
const currentTask = ref({})

const handleView = (row) => {
  currentTask.value = { ...row }
  viewDialogVisible.value = true
}

const handleAdd = () => {
  editDialogTitle.value = "添加任务";
  currentTask.value = {
    path_id: 0,
    day_number: 1,
    is_required: 0
  };
  editDialogVisible.value = true;
}

const handleEdit = (row) => {
  editDialogTitle.value = "编辑任务";
  currentTask.value = { ...row }
  editDialogVisible.value = true
}

const handleRemove = async (taskId) => {
  try {
    await axiosObject.delete(`task/${taskId}`);
    await findTaskList();
  } catch (error) {
    console.error("删除失败:", error);
  }
}

const handleSave = async () => {
  try {
    if (currentTask.value.task_id) {
      // 修改任务
      await axiosObject.put(`task/${currentTask.value.task_id}`, currentTask.value);
    } else {
      // 新增任务
      await axiosObject.post("task", currentTask.value);
    }
    await findTaskList();
    editDialogVisible.value = false
  } catch (error) {
    console.error("保存失败:", error);
  }
}

const findTaskList = async () => {
  try {
    const result = await axiosObject.get("task");
    console.log("任务数据:", result.data.resultData);
    tableData.value = result.data.resultData || [];
  } catch (error) {
    console.error("获取任务列表失败:", error);
    tableData.value = [];
  }
}

onMounted(() => {
  console.log("组件挂载完成，开始加载任务列表");
  findTaskList();
});
</script>
